package com.solocredit.finance.utils.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.solocredit.finance.R
import com.solocredit.finance.databinding.DialogSignBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.utils.event.MullaEventType

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/8 15:50
 * @Annotation：签到成功提示dialog
 */
class SignDialog: ProDialog {

    private lateinit var binding: DialogSignBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogSignBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.btnOk) //兑换优惠券
        setOnClickListener(R.id.imgClose)//关闭
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnOk -> {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_SIGN_SUCCESS_OK,eventRemark)
                mCallback?.onClick(v)
            }
            R.id.imgClose -> {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_SIGN_SUCCESS_CLOSE,eventRemark)
            }
        }
    }

    /**
     * 设置获得的积分
     */
    fun showMullaPoints(ponits: String){
        val ponitText = String.format(context.getString(R.string.sign_point), ponits)
        val showText = String.format(context.getString(R.string.sign_point_today),ponitText)
        val startIndex = showText.indexOf(ponitText)

        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FF8027FF")), startIndex, startIndex + ponitText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + ponitText.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textPoints.text = spString
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
        //dismiss
        fun onDismiss()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addMullaEventAction(MullaEventType.DIALOG.MULLA_DIALOG_SIGN_SUCCESS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        mCallback?.onDismiss()
        HttpHelper.addMullaEventAction(MullaEventType.DIALOG.MULLA_DIALOG_SIGN_SUCCESS_DISMISS,eventRemark)
    }
}